package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.ModelKit;
import appbase.jfinal.ext.RecordKit;
import appbase.utils.JsonUtil;
import dshop.AppConst;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Area;
import dshop.model.activity.Activity;
import dshop.model.company.Company;
import dshop.model.member.MemberGeneralInfo;

/**
 * 活动管理
 */
@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class ActivityAction extends ApiBaseController {

	public void list() {
		String keyWord = getPara("keyWord", "").trim();
		String ckeckStatus = getPara("checkStatus", "").trim();

		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];

		String select = " select id, create_account_id, title, content, tag, people_max_number, people_min_number, "
				+ " people_vip_level, people_account_level, "
				+ " DATE_FORMAT(start_date_time,'%Y-%m-%d %H:%i') start_date_time, "
				+ " DATE_FORMAT(end_date_time,'%Y-%m-%d %H:%i') end_date_time, "
				+ " DATE_FORMAT(check_date_time ,'%Y-%m-%d %H:%i') check_date_time, "
				+ " DATE_FORMAT(create_date_time ,'%Y-%m-%d %H:%i') create_date_time, "
				+ " check_status,  activity_status,is_delete,sold_out ";
		String where = " from activity where 1=1 ";
		String orderBy = " ";

		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and ( title like ? or content like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (!ckeckStatus.isEmpty()) {
			if (ckeckStatus.equals("END")) {
				where += " and sold_out = 1 ";
				where += " and is_delete = 'NO' ";
			} else if (ckeckStatus.equals("DELETE")) {
				where += " and is_delete = 'YES' ";
			} else {
				where += " and check_status = ? ";
				where += " and is_delete = 'NO' ";
				params.add(ckeckStatus);
			}
		}
		if (!columnOrder.isEmpty()) {
			orderBy += "order by " + columnProp + " " + columnOrder;
		} else {
			orderBy = " order by start_date_time desc";
		}
		page(select, where + orderBy, params.toArray());
		renderSuccess();
	}

	public void recommendList() {
		String keyWord = getPara("keyWord", "").trim();

		String select = " select id, create_account_id, title, recommend,content, tag, people_max_number, people_min_number, "
				+ " people_vip_level, people_account_level, "
				+ " DATE_FORMAT(start_date_time,'%Y-%m-%d %H:%i') start_date_time, "
				+ " DATE_FORMAT(end_date_time,'%Y-%m-%d %H:%i') end_date_time, "
				+ " DATE_FORMAT(check_date_time ,'%Y-%m-%d %H:%i') check_date_time, "
				+ " DATE_FORMAT(create_date_time ,'%Y-%m-%d %H:%i') create_date_time, "
				+ " check_status,  activity_status,is_delete,sold_out ";
		String where = " from activity where is_delete = 'NO' and sold_out = 0 and check_status = 'PASS' and activity_status = 'BE' ";

		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and ( title like ? or content like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		String orderBy = " order by recommend desc";
		page(select, where + orderBy, params.toArray());
		renderSuccess();
	}

	public void settingRecommend() {
		int activityId = getParaToInt("id");
		Db.update("update activity set recommend = 1 where id = ? ", activityId);
		renderSuccess("设置成功");
	}

	public void cancelRecommend() {
		int activityId = getParaToInt("id");
		Db.update("update activity set recommend = 0 where id = ? ", activityId);
		renderSuccess("取消成功");
	}

	@SuppressWarnings("unchecked")
	public void detail() {
		int activityId = getParaToInt("id");

		Activity activity = Activity.dao.findById(activityId);
		setAttr("activity", activity);
		Integer accountId = activity.getInt("create_account_id");
		activity.put("account", MemberGeneralInfo.dao.findById(accountId));
		activity.put("company", Company.getCompanyInfoByAccountId(accountId));

		Area.setNameToM(activity, "province_code, city_code, area_code", "province, city, area");

		ModelKit.putDateFormat(activity, "start_date_time", "start_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(activity, "end_date_time", "end_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(activity, "check_date_time", "check_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(activity, "create_date_time", "create_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(activity, "update_date_time", "update_date_time", "yyyy-MM-dd HH:mm");
		ModelKit.putDateFormat(activity, "activity_cancel_date_time", "activity_cancel_date_time", "yyyy-MM-dd HH:mm");

		String coverJson = activity.getStr("cover");
		List<String> coverImgList = (List<String>) JsonUtil.toList(coverJson);
		activity.put("coverImgList", coverImgList);

		renderSuccess();
	}

	public void delete() {
		int activityId = getParaToInt("id");

		Activity.delete(activityId);

		renderSuccess("删除成功");
	}

	public void checkStatusPass() {
		int activityId = getParaToInt("id");
		String remark = getPara("remark", "").trim();

		String checkStatus = AppConst.CHECK_STATUS_PASS;

		Activity.setCheckStatus(activityId, checkStatus, remark);

		renderSuccess("成功设置为已通过");
	}

	public void checkStatusFail() {
		int activityId = getParaToInt("id");
		String remark = getPara("remark", "").trim();
		Activity.setCheckStatusFail(activityId, remark);

		renderSuccess("已设置为不通过");
	}

	public void giveJst() {

		String phone = getPara("phone", "").trim();
		int num = getParaToInt("num");

		Activity activity = new Activity();
		Activity a = activity.findIdByPhone(phone);
		if (a == null) {
			renderFail("没有查询到该用户！");
			return;
		}
		int userId = a.getInt("account_id");
		int count = activity.checkGiveToo(userId).getInt("count");
		if (count == 0) {
			activity.insertJst(userId, num);
		} else {
			activity.updateJst(userId, num);
		}
		activity.pushPond(num);
		renderSuccess("赠送成功！");

	}

	public void getPushNum() {
		String pushNum = Activity.queryPushPond();
		renderSuccess(pushNum);
	}

}
